package controller;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import bean.User;
import bean.model.PageModel;
import service.StaffService;
import util.MD5Util;
import util.PageUtil;

@Controller
public class StaffManagerController {
	StaffService staffService = new StaffService();
	
	@RequestMapping("/staffManager")
	public String toStaffManager(HttpServletRequest request){	
		if (request.getSession().getAttribute("staffPageModel") == null){
			PageModel createPage = PageUtil.createPage(15, staffService.getStaffNum(), 1);
			List<User> queryStaffByPage = staffService.queryStaffByPage(createPage);	
			request.getSession().setAttribute("sss", queryStaffByPage);
			request.getSession().setAttribute("staffPageModel", createPage);
		}
		return "staffManager";
	}
	
	@RequestMapping("/staffManagerSearch")
	public ResponseEntity<String> staffManagerSearch(HttpServletRequest request) throws UnsupportedEncodingException{
		List <User> list = new ArrayList<User>();
		String userName = request.getParameter("userName");
		String sex = request.getParameter("sex");
		String ageFrom = request.getParameter("ageFrom");
		String ageTo = request.getParameter("ageTo");
		String rigor = request.getParameter("chk");
		list = staffService.getStaffSearchList(userName, sex, ageFrom, ageTo,rigor);	
		request.setAttribute("list",list);//绑定数据	
		
		String json = JSON.toJSONString(list);
		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
		responseHeaders.setContentType(mediaType);
		responseHeaders.setContentType(new MediaType("text", "json", Charset.forName("UTF-8")));
		ResponseEntity<String> responseResult = new ResponseEntity<String>(json, responseHeaders, HttpStatus.OK);
		System.out.println(json);
		return responseResult;
	}
	
	@RequestMapping("/staffManagerInformation")
	public String staffManagerInformation(HttpServletRequest request){
		String username = request.getParameter("username");	
		request.setAttribute("username", username);
		return "test";
	}
	
	@RequestMapping("/staffManagerDelete")
	public void staffManagerDelete(HttpServletRequest request){
		StaffService ss = new StaffService();

		String deleteName = request.getParameter("deleteName");
		System.out.println(deleteName);
		ss.deleteStaff(deleteName);
		
		return ;
	}
	
	@RequestMapping("/staffManagerInsert")
	public ResponseEntity<JSONObject> staffManagerInsert(HttpServletRequest request){
		String username = request.getParameter("username");
		String userRealName = request.getParameter("userRealName");
		String userSex = request.getParameter("userSex");
		String userBirthday = request.getParameter("userBirthday");
		String userPhoneNumber = request.getParameter("userPhoneNumber");
		int intPhoneNumber = Integer.parseInt(userPhoneNumber);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("staffManager");
		
		System.out.println(username);
		System.out.println(userRealName);
		System.out.println(userSex);
		System.out.println(userBirthday);
		System.out.println(userPhoneNumber);
	
		String md5password = MD5Util.MD5("123456");
		
		staffService.addStaff(username, md5password, userBirthday, userRealName, userSex, intPhoneNumber);
		JSONObject json = new JSONObject();
		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
		responseHeaders.setContentType(mediaType);
		responseHeaders.setContentType(new MediaType("text", "json", Charset.forName("UTF-8")));
		ResponseEntity<JSONObject> responseResult = new ResponseEntity<JSONObject>(json, responseHeaders,
				HttpStatus.OK);
		return responseResult;
	}
	
	@RequestMapping("/staffManagerFirstPage")
	public ResponseEntity<String> staffManagerFirstPage(HttpServletRequest request) throws UnsupportedEncodingException{
	
		int staffPageSize = Integer.parseInt(request.getParameter("staffPageSize"));
	
		PageModel createPage = PageUtil.createPage(staffPageSize, staffService.getStaffNum(), 1);
		List<User> queryStaffByPage = staffService.queryStaffByPage(createPage);	
		request.setAttribute("list",queryStaffByPage);//绑定数据	
		
		String json = JSON.toJSONString(queryStaffByPage);
		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
		responseHeaders.setContentType(mediaType);
		responseHeaders.setContentType(new MediaType("text", "json", Charset.forName("UTF-8")));
		ResponseEntity<String> responseResult = new ResponseEntity<String>(json, responseHeaders, HttpStatus.OK);
		System.out.println(json);
		return responseResult;
	}
	
	@RequestMapping("/staffManagerLastPage")
	public ResponseEntity<String> staffManagerLastPage(HttpServletRequest request) throws UnsupportedEncodingException{
	
		int staffPageSize = Integer.parseInt(request.getParameter("staffPageSize"));
		
		PageModel createPage = PageUtil.createPage(staffPageSize, staffService.getStaffNum(), staffService.getStaffNum()/staffPageSize);
		List<User> queryStaffByPage = staffService.queryStaffByPage(createPage);	
		request.setAttribute("list",queryStaffByPage);//绑定数据	
		
		String json = JSON.toJSONString(queryStaffByPage);
		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
		responseHeaders.setContentType(mediaType);
		responseHeaders.setContentType(new MediaType("text", "json", Charset.forName("UTF-8")));
		ResponseEntity<String> responseResult = new ResponseEntity<String>(json, responseHeaders, HttpStatus.OK);
		System.out.println(json);
		return responseResult;
	}
	
	@RequestMapping("/staffManagerPrePage")
	public ResponseEntity<String> staffManagerPrePage(HttpServletRequest request) throws UnsupportedEncodingException{
	
		int staffPageSize = Integer.parseInt(request.getParameter("staffPageSize"));
		int curPage = Integer.parseInt(request.getParameter("staffCurPage"));
	
		PageModel createPage = PageUtil.createPage(staffPageSize, staffService.getStaffNum(),curPage - 1);
		List<User> queryStaffByPage = staffService.queryStaffByPage(createPage);	
		request.setAttribute("list",queryStaffByPage);//绑定数据	
		
		String json = JSON.toJSONString(queryStaffByPage);
		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
		responseHeaders.setContentType(mediaType);
		responseHeaders.setContentType(new MediaType("text", "json", Charset.forName("UTF-8")));
		ResponseEntity<String> responseResult = new ResponseEntity<String>(json, responseHeaders, HttpStatus.OK);
		System.out.println(json);
		return responseResult;
	}
	
	@RequestMapping("/staffManagerNextPage")
	public ResponseEntity<String> staffManagerNextPage(HttpServletRequest request) throws UnsupportedEncodingException{
	
		int staffPageSize = Integer.parseInt(request.getParameter("staffPageSize"));
		int curPage = Integer.parseInt(request.getParameter("staffCurPage"));
		
		PageModel createPage = PageUtil.createPage(staffPageSize, staffService.getStaffNum(),curPage + 1);
		List<User> queryStaffByPage = staffService.queryStaffByPage(createPage);	
		request.setAttribute("list",queryStaffByPage);//绑定数据	
		
		String json = JSON.toJSONString(queryStaffByPage);
		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
		responseHeaders.setContentType(mediaType);
		responseHeaders.setContentType(new MediaType("text", "json", Charset.forName("UTF-8")));
		ResponseEntity<String> responseResult = new ResponseEntity<String>(json, responseHeaders, HttpStatus.OK);
		System.out.println(json);
		return responseResult;
	}
	
	@RequestMapping("/staffManagerJumpPage")
	public ResponseEntity<String> staffManagerJumpPage(HttpServletRequest request) throws UnsupportedEncodingException{
	
		int staffPageSize = Integer.parseInt(request.getParameter("staffPageSize"));
		int curPage = Integer.parseInt(request.getParameter("staffCurPage"));
		
		PageModel createPage = PageUtil.createPage(staffPageSize, staffService.getStaffNum(),curPage);
		List<User> queryStaffByPage = staffService.queryStaffByPage(createPage);	
		request.setAttribute("list",queryStaffByPage);//绑定数据	
		
		String json = JSON.toJSONString(queryStaffByPage);
		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
		responseHeaders.setContentType(mediaType);
		responseHeaders.setContentType(new MediaType("text", "json", Charset.forName("UTF-8")));
		ResponseEntity<String> responseResult = new ResponseEntity<String>(json, responseHeaders, HttpStatus.OK);
		System.out.println(json);
		return responseResult;
	}
	
	
	
}
